package edu.cs346.group5.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.JLabel;

import edu.cs346.group5.datastores.Topic;
import edu.cs346.group5.datastores.User;
import edu.cs346.group5.logic.Home;
import edu.cs346.group5.ui.forumpages.view_topic;
import edu.cs346.group5.ui.userpages.user_page;

public class ResultsTable extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	static ArrayList<String[]> alsa;

	/**
	 * Launch the application.
	 */
	public static void main(final ArrayList<String[]> args, final boolean isTopic) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					alsa = args;
					ResultsTable frame = new ResultsTable(isTopic);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ResultsTable(final boolean isTopic) {
		setTitle("Results");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		setJMenuBar(MenuFactory.getMenu(this));
		
		

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow]", "[][grow]"));
		System.out.println(alsa.size());
		String [] nomes = new String[alsa.get(0).length];

		JLabel lblTheSetOf = new JLabel("The set of results is Empty");
		lblTheSetOf.setVisible(false);
		contentPane.add(lblTheSetOf, "cell 0 0,alignx center");
		nomes = alsa.remove(0);
		
		String[][] alsa2 = new String[alsa.size()][alsa.get(0).length];
		if ( alsa.size() == 0 )
			lblTheSetOf.setVisible(true);
		for (int i = 0; i < alsa.size(); i++) {
			alsa2[i] = alsa.get(i);
		}
		DefaultTableModel dtm = new DefaultTableModel(alsa2,
				nomes);
		

		

		table = new JTable(dtm);
		JScrollPane pane = new JScrollPane(table); 
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				JTable target = (JTable)e.getSource();
			    int row = target.getSelectedRow();
			    int column = target.getSelectedColumn();
			    if(!isTopic){
				    String email = target.getValueAt(row, 2).toString();
				    User user = Home.GetUserByEmail(email);
				    user_page x = new user_page(user, false);
				    x.setVisible(true);
				    dispose();
			    }
			    else{
			    	String name = target.getValueAt(row, 2).toString();
			    	DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				    java.util.Date date = null;
					try {
						date = formatter.parse(target.getValueAt(row, 0).toString());
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					Date sqlDate = new Date(date.getTime());
					Topic topic = new Topic();
					topic.setTopicName(name);
					topic.setPostDate(sqlDate);
					view_topic x = new view_topic(Home.GetTopicByNameDate(topic));
					x.setVisible(true);
					dispose();
			    }
			    
			}
		});
		contentPane.add(pane, "cell 0 1,grow");

	}

}
